package com.platform.parser;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.platform.domain.Client;
import com.platform.request.BaseRequest;
import com.platform.request.HeartBeatRequest;
import com.platform.response.BaseResponse;
import com.platform.response.FrameTransferResponse;
import com.platform.response.GroupChatAskForResponse;
import com.platform.response.GroupChatReleaseResponse;
import com.platform.response.GroupChatResponse;
import com.platform.response.HeartBeatResponse;


public class BaseParser {
	
	private Gson gson;
	
	public BaseParser(){
		GsonBuilder builder = (new GsonBuilder()).disableHtmlEscaping();
		gson = builder.create();
	}
	
	public BaseResponse parse(String json){
		
		BaseRequest request = gson.fromJson(json, BaseRequest.class);
		return handlerRequest(request.MessageType,json);
	}
	
	protected BaseResponse handlerRequest(String messageType,String json){
		
		BaseResponse response = null;
		switch(messageType){
			case BaseResponse.MsgType.HeartBeatResponse:
				response = gson.fromJson(json,HeartBeatResponse.class);
				break;
			case BaseResponse.MsgType.FrameTransferResponse:
				response = gson.fromJson(json, FrameTransferResponse.class);
				break;
			case BaseResponse.MsgType.GroupChatAskForResponse:
				response = gson.fromJson(json, GroupChatAskForResponse.class);
				break;
			case BaseResponse.MsgType.GroupChatReleaseResponse:
				response = gson.fromJson(json, GroupChatReleaseResponse.class);
				break;
			case BaseResponse.MsgType.GroupChatResponse:
				response = gson.fromJson(json, GroupChatResponse.class);
				break;
		}
		return response;
	}
	
}
